﻿using System;
using System.Linq;
using System.ServiceModel;
using System.Web.Mvc;
using NerdDinnerMVC.Models;
using NerdDinnerMVC.NerdDinnerService;


namespace NerdDinnerMVC.Controllers
{

  
    public class DinnersController : Controller
    {


        public DinnersController()
        {
        }


        public ActionResult Index(int page = 0)
        {
            var dinners = new DinnerBusinessObject().FindUpcomingDinners(page);
            return View("Index", dinners);
        }

        public ActionResult Find()
        {
            return View("Find");
        }

        public ActionResult Details(int id)
        {
            var dinner = new DinnerBusinessObject().GetDinner(id);
            if (dinner == null)
            {
                return View("NotFound");
            }
            else
            {
                return View("Details", dinner);
            }
        }

        public ActionResult Delete(int id)
        {
            var dinnerBusinessObject = new DinnerBusinessObject();
            var dinner = dinnerBusinessObject.GetDinner(id);
          if (dinner == null)
          {   
              return View("NotFound");
          }
          if (!dinnerBusinessObject.IsAllowedToModifiedBy(dinner, User.Identity.Name))
          {
              return View("InvalidOwner");
          }
          else
          {
              return View(dinner);
          }
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            var dinnerServiceProxy = new DinnerBusinessObject();
            var dinner = dinnerServiceProxy.GetDinner(id);
            if (dinner == null)
            {
                return View("NotFound");
            }

            if (!dinnerServiceProxy.IsAllowedToModifiedBy(dinner, User.Identity.Name))
            {
                return View("InvalidOwner");
            }

            return View(new DinnerFormViewModel(dinner));
        }

        [HttpPost, Authorize]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            // Retrieve existing dinner
            var dinnerServiceProxy = new DinnerBusinessObject();
            var dinner = dinnerServiceProxy.GetDinner(id);
            if (dinner == null)
            {
                return View("NotFound");
            }
            if (!dinnerServiceProxy.IsAllowedToModifiedBy(dinner, User.Identity.Name))
            {
                return View("InvalidOwner");
            }

            // Update dinner with form posted values after validation
            if (TryUpdateModel(dinner, "Dinner"))
            {
                // Persist changes back to database
                dinnerServiceProxy.UpdateDinner(dinner);
                // Perform HTTP redirect to details page for the saved Dinner
                return RedirectToAction("Details", new { id = dinner.DinnerId });
            }
            return View(new DinnerFormViewModel(dinner));
        }

        [Authorize]
        public ActionResult Create()
        {
            var dinner = new DinnerDTO
                             {
                                 EventDate = DateTime.Now.AddDays(7)
                             };
            return View(new DinnerFormViewModel(dinner));
        }

        [HttpPost, Authorize]
        public ActionResult Create(DinnerDTO dinner)
        {

            if (ModelState.IsValid)
            {
                var dinnerServiceProxy = new DinnerBusinessObject();
                dinner.HostedBy = User.Identity.Name;
                int id = dinnerServiceProxy.CreateDinner(dinner);
                return RedirectToAction("Details", new { id = id });
            }
            return View(new DinnerFormViewModel(dinner));
        }


        [HttpPost]
        public ActionResult Delete(int id, string confirmButton)
        {
            try
            {
                var dinnerServiceProxy = new DinnerBusinessObject();
                DinnerDTO dinner = dinnerServiceProxy.GetDinner(id);

                if (!dinnerServiceProxy.IsAllowedToBeDeletedBy(dinner, User.Identity.Name))
                {
                    return View("InvalidOwner");
                }
                dinnerServiceProxy.DeleteDinner(id);
            }
            catch (FaultException<NotFoundFault> notFoundFault)
            {
                return View("NotFound");
            }
            catch (Exception ex)
            {
                throw;
            }

            return View("Deleted");
        }
    }
}